# !/usr/bin/python3
# -*- coding: utf-8 -*-
import urllib.parse

import httpx
from lxml import etree

import httpx
import execjs
import re

# https://www.mangabz.com/m13866/#ipg8


def get_content(url, headers):
    client = httpx.Client(http2=True, verify=False)
    response = client.get(url=url, headers=headers)
    return response.text


class ComicsDetail:

    def __init__(self):
        self.base_url = "https://www.mangabz.com/"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
            "Referer": self.base_url,
            "Cookie": "image_time_cookie=13866|637270678077155170|2",
        }

    def run(self, cid):
        # https://www.mangabz.com/m13866/
        # https://www.mangabz.com/m13866-p1/chapterimage.ashx?cid=13866&page=1&key=&_cid=13866&_mid=139&_dt=2021-07-20+11%3A47%3A37&_sign=012b5fbf9822029815004b8a58ecf96b
        # url = "https://www.mangabz.com/m13866/" + page_index
        # url = "https://www.mangabz.com/m13866/chapterimage.ashx?cid=13866&page=1&key=&_cid=13866&_mid=139&_dt=2021-07-20+11%3A47%3A37&_sign=012b5fbf9822029815004b8a58ecf96b"

        url = self.base_url + cid + "/"
        mangabz_cid, mangabz_mid, mangabz_viewsign_dt, mangabz_viewsign, page_total = self.get_chapter_argv(url)

        # url = url + "chapterimage.ashx?" + "cid=%s&page=%s&key=&_cid=%s&_mid=%s&_dt=%s&_sign=%s" % (mangabz_cid, 1, mangabz_cid, mangabz_mid, urllib.parse.quote(mangabz_viewsign_dt), mangabz_viewsign)
        # print(url)
        # # self.get_chapter_argv(url)
        # self.headers["Referer"] = url
        # js_content = get_content(url, self.headers)
        # print(js_content)
        # text = str(content, 'utf-8')
        # print(text)

        # print(execjs.eval(js_content))

        # js = """
        # function dm5imagefun(){var cid=13866;var key='0e58e98ad60bb253434a7077822b13b5';var pix="https://image.mangabz.com/1/139/13866";var pvalue=["/1_1154.jpg","/2_2787.jpg"];for(var i=0;i<pvalue.length;i++){pvalue[i]=pix+pvalue[i]+'?cid=13866&key=0e58e98ad60bb253434a7077822b13b5&uk='}return pvalue}
        # """
        # ctx = execjs.compile(js)
        # print(ctx.call("dm5imagefun"))


    def get_chapter_argv(self, url):
        print(url)
        # url = "https://www.mangabz.com/m13866/chapterimage.ashx?cid=13866&page=1&key=&_cid=13866&_mid=139&_dt=2021-07-20+11%3A47%3A37&_sign=012b5fbf9822029815004b8a58ecf96b"
        # https://www.mangabz.com/m13866/chapterimage.ashx?cid=13866&page=1&key=&_cid=13866&_mid=139&_dt=2021-07-20%2015%3A00%3A00&_sign=e80a732b339ec5bdea51eeb316995366
        content = get_content(url, self.headers)
        # print(content)

        mangabz_cid = re.findall("MANGABZ_CID=(.*?);", content)[0]
        mangabz_mid = re.findall("MANGABZ_MID=(.*?);", content)[0]
        page_total = re.findall("MANGABZ_IMAGE_COUNT=(.*?);", content)[0]
        mangabz_viewsign_dt = re.findall("MANGABZ_VIEWSIGN_DT=\"(.*?)\";", content)[0]
        mangabz_viewsign = re.findall("MANGABZ_VIEWSIGN=\"(.*?)\";", content)[0]

        print(mangabz_cid, mangabz_mid, mangabz_viewsign_dt, mangabz_viewsign, page_total, sep='|')
        return mangabz_cid, mangabz_mid, mangabz_viewsign_dt, mangabz_viewsign, page_total


if __name__ == '__main__':
    spider = ComicsDetail()
    spider.run("m13866")

